package com.frxuexi.mdm2206.view

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Email
import androidx.compose.material.icons.filled.Home
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.mdm2206.MyNavHost

sealed class NavItem(val title: String, val img: ImageVector, val route: String) {
    object Message : NavItem("消息", Icons.Default.Email, RoutePath.MASSAGE)
    object Home : NavItem("门户", Icons.Default.Home, RoutePath.HOME)
    object Mine : NavItem("我", Icons.Default.AccountCircle, RoutePath.MY)
}

@Composable
fun MainPage(navController:NavHostController= rememberNavController()) {
    val list = listOf<NavItem>(NavItem.Message, NavItem.Home, NavItem.Mine)
//    val navController = rememberNavController()
    //获取当前导航的返回堆栈信息
    val entryAsState by navController.currentBackStackEntryAsState()
    //获取路径信息
    val destination = entryAsState?.destination
    Scaffold(bottomBar = {
        //二级页面不显示底部导航
        if (destination?.hierarchy?.any {
                list.map { navItem -> navItem.route }.contains(it.route)
            } == true) {
            NavigationBar {
                list.forEach { nav ->
                    NavigationBarItem(selected = destination?.hierarchy?.any { it.route == nav.route } == true,
                        onClick = {
                            navController.navigate(nav.route) {
                                //保存状态
                                popUpTo(navController.graph.startDestinationId) {
                                    saveState = true
                                }
                                //栈顶复用
                                launchSingleTop = true
                                //状态恢复
                                restoreState = true
                            }
                        },
                        icon = { Icon(imageVector = nav.img, contentDescription = "") },
                        label = { Text(text = nav.title) })
                }
            }
        }
    }) {
        MyNavHost(navHostController = navController, it = it)
    }
}